package com.lq.ecoldchain.ui.mymileage.ranking

import android.arch.lifecycle.MutableLiveData
import com.lq.ecoldchain.base.viewmodel.BaseViewModel
import com.lq.ecoldchain.entity.MileageRankingBean
import com.lq.ecoldchain.internet.BaseObserver
import com.lq.ecoldchain.util.toast

/*
*2019/1/5 0005  20:13
*function by lq
*/
class MileageRankingViewModel : BaseViewModel() {
    private var page = 1
    private val limit = 10

    val refreshing by lazy { MutableLiveData<Boolean>() }
    val myMileage by lazy { MutableLiveData<String>() }
    val list by lazy { ArrayList<MileageRankingBean>() }
    val adapter by lazy { RankingAdapter(list) }


    init {
        mileageRanking()
        myMileageRanking()
    }

    fun refreshData() {
        refreshing.value = true
        mileageRanking()
        myMileageRanking()
    }

    private fun mileageRanking() {
        BaseNetCore.ApiServiceImpl.mileageRanking(page, limit).compose(RxTransformer.switchSchedulers()).subscribe(object : BaseObserver<List<MileageRankingBean>>() {
            override fun onSuccess(t: List<MileageRankingBean>?) {
                list.clear()
                if (!t.isNullOrEmpty())
                    list.addAll(t)
                adapter.notifyDataSetChanged()
                if(refreshing.value ==true){
                    toast { "刷新成功" }
                }
                refreshing.value = false
            }

            override fun onFail(code: String) {
                refreshing.value = false
            }
        })
    }

    private fun myMileageRanking() {
        BaseNetCore.ApiServiceImpl.myMileageRanking().compose(RxTransformer.switchSchedulers()).subscribe(object : BaseObserver<String>() {
            override fun onSuccess(t: String?) {
                myMileage.value = t
            }

            override fun onFail(code: String) {

            }
        })
    }
}